<?php


namespace App\Supports\Formatter;


use Illuminate\Database\Eloquent\Model;

class ArrayFormatter
{

    /**
     * 一维数组转key
     * @param $oldData
     * @param $keyMap
     * @param $canHasNull
     * @param $keyDefault
     * @return array
     */
    public static function convertKEY($oldData, $keyMap, $canHasNull = true, $keyDefault = null)
    {
        $newData = [];
        foreach ($keyMap as $newKey => $oldKey) {
            if ($oldKey instanceof \Closure) {
                $newData[$newKey] = $oldKey($oldData);
                continue;
            }
            if (is_object($oldData)) {
                try {
                    if (strpos($oldKey, '.')) {
                        $keyValue = explode('.', $oldKey);
                        $newData[$newKey] = $oldData->{$keyValue[0]} ? $oldData->{$keyValue[0]}->{$keyValue[1]} : '';
                    } else {
                        $newData[$newKey] = $oldData->$oldKey;
                    }
                } catch (\Throwable $throwable) {
                    //不存在的属性则跳过
                    continue;
                }
            } else {
                if (strpos($oldKey, '.')) {
                    $keyValue = explode('.', $oldKey);
                    $newData[$newKey] = $oldData[$keyValue[0]][$keyValue[1]] ?? '';
                } else if (strpos($oldKey, 'default:') === 0) {
                    $keyValue = explode(':', $oldKey);
                    $newData[$newKey] = $keyValue[1] ?? '';
                } else if (strpos($oldKey, 'number:') === 0) {
                    $keyValue = explode(':', $oldKey);
                    $newData[$newKey] = (isset($keyValue[1]) && isset($oldData[$keyValue[1]])) ? (is_numeric($oldData[$keyValue[1]]) ? $oldData[$keyValue[1]] : 0) : 0;
                } else if (!key_exists($oldKey, $oldData)) {
                    if ($keyDefault !== null) {
                        $newData[$newKey] = $keyDefault;
                    }
                } else {
                    if ($canHasNull) {
                        $newData[$newKey] = $oldData[$oldKey];
                    } else {
                        $newData[$newKey] = $oldData[$oldKey] === NULL ? '' : $oldData[$oldKey];
                    }
                }
            }
        }
        return $newData;
    }

    /**
     * 二维数组转key
     * @param $oldDatas
     * @param $keyMap
     * @param $canHasNull
     * @param $keyDefault
     * @return array
     */
    public static function arrayConvertKEY($oldDatas, $keyMap, $canHasNull = true, $keyDefault = null)
    {
        if (empty($keyMap)) {
            return $oldDatas;
        }
        $array = [];
        foreach ($oldDatas as $oldData) {
            $array[] = self::convertKEY($oldData, $keyMap, $canHasNull, $keyDefault);
        }
        return $array;
    }

    /**
     * tree转Key
     * @param $oldData
     * @param $keyMap
     * @param $newKeyNodeName
     * @return array
     */
    public static function treeConvertKey($oldData, $keyMap, $newKeyNodeName)
    {
        $newData = [];
        if ($oldData[$keyMap[$newKeyNodeName]] !== null) {
            $item = [];
            foreach ($oldData[$keyMap[$newKeyNodeName]] as $nodeData) {
                $item[] = self::treeConvertKey($nodeData, $keyMap, $newKeyNodeName);
            }
            $newData[$newKeyNodeName] = $item;
        }
        $convertKeyMap = $keyMap;
        unset($convertKeyMap[$newKeyNodeName]);
        $newData = array_merge($newData, self::convertKEY($oldData, $convertKeyMap));
        return $newData;
    }

    /**
     * tree数组转Key
     * @param $oldDatas
     * @param $keyMap
     * @param $newKeyNodeName
     * @return array
     */
    public static function treeArrayConvertKey($oldDatas, $keyMap, $newKeyNodeName)
    {
        $array = [];
        foreach ($oldDatas as $oldData) {
            $array[] = self::treeConvertKey($oldData, $keyMap, $newKeyNodeName);
        }
        return $array;
    }

}
